﻿using BL_Client.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class Placements : System.Web.UI.Page
    {
        static BL_Client.IBL bl = new BL_Client.BL();
        static User user;

        public Placements()
        {
            user = bl.GetUserByEmail(User.Identity.Name);
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (userIsAdmin(user)) //check the role of the user
                this.Page.MasterPageFile = "~/Admin.Master";
            else
                this.Page.MasterPageFile = "~/AvBait.Master";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (userIsAdmin(user))
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#PlacementsTableContainer').jtable({
                                                    title: 'Table of Placement',
                                                    actions: {
                                                        listAction: '/Placements.aspx/PlacementsList',
                                                        createAction: '/Placements.aspx/CreatePlacement',
                                                        updateAction: '/Placements.aspx/UpdatePlacement',
                                                        deleteAction: '/Placements.aspx/DeletePlacement'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        StudentID: {
                                                            title: 'Student Name',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetStudents'
                                                        },
                                                        RoomID: {
                                                            title: 'Room Name',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetRooms'
                                                        },
                                                        Enterance: {
                                                            title: 'Enterance Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        Departure: {
                                                            title: 'Departure Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeStart: {
                                                            title: 'Charge Start Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeEnd: {
                                                            title: 'Charge End Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeID: {
                                                            title: 'Charge',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetCharges'
                                                        },   
                                                        Comments: {
                                                            title: 'Comments',
                                                            width: '10%',
                                                            type: 'textarea'
                                                        }
                                                    },
                                                    formCreated: function (event, data) {
                                                                    data.form.find('input[name=""ChargeStart""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""ChargeEnd""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Enterance""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Departure""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Email""]').addClass('validate[required,custom[email]');
                                                                    data.form.find('input[name=""Password""]').addClass('validate[required]');
                                                                    data.form.validationEngine();
                                                    },
                                                    formSubmitting: function (event, data) {
                                                        return data.form.validationEngine('validate');
                                                    },
                                                    formClosed: function (event, data) {
                                                        data.form.validationEngine('hide');
                                                        data.form.validationEngine('detach');
                                                    }
                                                });

                                                $('#PlacementsTableContainer').jtable('load');
                                            });
                                        </script>";
            else if (user.Role == Role.AvBait)
                jTableScript.InnerHtml = @"<script type=""text/javascript"">
                                            $(document).ready(function () {
                                                $('#PlacementsTableContainer').jtable({
                                                    title: 'Table of Placement',
                                                    actions: {
                                                        listAction: '/Placements.aspx/PlacementsList'
                                                    },
                                                    fields: {
                                                        ID: {
                                                            key: true,
                                                            list: false
                                                        },
                                                        StudentID: {
                                                            title: 'Student Name',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetStudents'
                                                        },
                                                        RoomID: {
                                                            title: 'Room Name',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetRooms'
                                                        },
                                                        Enterance: {
                                                            title: 'Enterance Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        Departure: {
                                                            title: 'Departure Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeStart: {
                                                            title: 'Charge Start Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeEnd: {
                                                            title: 'Charge End Date',
                                                            width: '10%',
                                                            type: 'date',
                                                            displayFormat: 'dd/mm/yy',
                                                            create: true,
                                                            edit: false,
                                                            sorting: false
                                                        },
                                                        ChargeID: {
                                                            title: 'Charge',
                                                            width: '10%',
                                                            options: '/Placements.aspx/GetCharges'
                                                        },   
                                                        Comments: {
                                                            title: 'Comments',
                                                            width: '10%',
                                                            type: 'textarea'
                                                        }
                                                    },
                                                    formCreated: function (event, data) {
                                                                    data.form.find('input[name=""ChargeStart""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""ChargeEnd""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Enterance""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Departure""]').addClass('validate[required]');
                                                                    data.form.find('input[name=""Email""]').addClass('validate[required,custom[email]');
                                                                    data.form.find('input[name=""Password""]').addClass('validate[required]');
                                                                    data.form.validationEngine();
                                                    },
                                                    formSubmitting: function (event, data) {
                                                        return data.form.validationEngine('validate');
                                                    },
                                                    formClosed: function (event, data) {
                                                        data.form.validationEngine('hide');
                                                        data.form.validationEngine('detach');
                                                    }
                                                });

                                                $('#PlacementsTableContainer').jtable('load');
                                            });
                                        </script>";
        }

        [WebMethod(EnableSession = true)]
        public static object PlacementsList()
        {
            try
            {
                //Get data from database
                List<Placement> placement = bl.GetAllPlacements().ToList();

                //Return result to jTable
                return new { Result = "OK", Records = placement, TotalRecordCount = placement.Count };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }

        }

        [WebMethod(EnableSession = true)]
        public static object CreatePlacement( Placement record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.AddPlacement(record))
                {
                    Placement addPlacement = record;
                    return new { Result = "OK", Record = addPlacement };
                }
                else
                {
                    return new { Result = "ERROR", Message = "Placement insertion error" };
                }
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object UpdatePlacement(Placement record)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.UpdatePlacement(record))
                {
                    return new { Result = "OK" };
                }
                else
                {
                    return new { Result = "ERROR", Message = "Placement insertion error" };
                }
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object DeletePlacement(int ID)
        {
            if (!userIsAdmin(user))
                return new { Result = "ERROR" };

            try
            {
                if (bl.RemovePlacement(ID))
                    return new { Result = "OK" };
                else
                {
                    return new { Result = "ERROR", Message = "Placement deletion error" };
                }
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object GetRooms()
        {
            try
            {
                var rooms = bl.GetAllRooms().Select(r => new { DisplayText = r.Dorm.Name + ", " + r.Number, Value = r.ID }).OrderBy(d => d.DisplayText);
                return new { Result = "OK", Options = rooms };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object GetCharges()
        {
            try
            {
                var Charges = bl.GetAllCharges().Select(d => new { DisplayText = d.Name, Value = d.ID }).OrderBy(d => d.DisplayText);
                return new { Result = "OK", Options = Charges };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }
        [WebMethod(EnableSession = true)]
        public static object GetStudents()
        {
            try
            {
                var Student = bl.GetAllStudents().Select(d => new { DisplayText = d.FirstName+' '+d.LastName, Value = d.ID }).OrderBy(d => d.DisplayText);
                return new { Result = "OK", Options = Student };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", Message = ex.Message };
            }
        }


        private static bool userIsAdmin(User user)
        {
            return (user.Role == Role.Admin);
        }
    }
}
     
 